#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QThread>
#include "data_processing.h"
#include "information.h"

Data_processing::Data_processing(QObject* parent)
    : QObject(parent)
{

}

Data_processing::~Data_processing()
{
    qDebug()<< "Data_processing::~Data_processing = " << QThread::currentThreadId();

}

void Data_processing::read_file(const QString &file_name_and_path)
{
    qDebug()<< "Data_processing::read_file = " << QThread::currentThreadId();

    QList<Information*>* list = read_data_from_txt_file(file_name_and_path);

    // 从文件中读取到的台站信息
    emit signal_frequency_informations(list);

    // 获取文件名（不包括文件路径及后缀名）
    emit signal_station_name(get_station_name(file_name_and_path));
}

void Data_processing::calculate_frequency(double frequency, QList<Information *> *list)
{
    qDebug()<< "Data_processing::calculate_frequency = " << QThread::currentThreadId();

    int count(list->count());
    for (int i(0); i != count; ++i) {
        double cal = frequency * 2 - list->at(i)->get_frequency();

        // 遍历台站频率进行互调计算
        for (int index(0); index != count; ++index) {
            QString number =
                    QString::number(qAbs(cal - list->at(index)->get_frequency()), 'f', 3);
            if (0.000 == number.toDouble()) {
                list->value(i)->set_is_inter_modulation(true);
                list->value(i)->set_inter_modulation_frequency(cal);
            }
        }

        // 遍历台站频率进行互调计算
        cal = list->at(i)->get_frequency() * 2 - frequency;
        for (int index(0); index != count; ++index) {
            QString number =
                    QString::number(qAbs(list->at(index)->get_frequency() - cal), 'f', 3);
            if (0.000 == number.toDouble()) {
                list->value(i)->set_is_inter_modulation(true);
                list->value(i)->set_inter_modulation_frequency(cal);
            }
        }
    }

    // 发射互调计算后的台站信息，如果有互调信息，更新对应频率的是否互调、互调频率
    emit signal_is_inter_modulation(list);
}

QList<Information *>* Data_processing::read_data_from_txt_file(const QString &file_name_and_path)
{
    QFile file(file_name_and_path);

    if ( file.open(QFile::ReadOnly)) {
        QTextStream in(&file);

        // 编码设置
        in.setCodec("utf-8");
        QString content("");

        QList<Information*>* station_information_list(new QList<Information*>());

        // 遍历文件每一行频率信息，将信息拆分保存到Information*中，并将Information*保存到
        //  列表station_information_list中
        while (0 != ((content = in.readLine()).length())) {
            station_information_list->append(split_data(content));
        }

        // 打开的文档内容为空，删除保存数据的实例，赋值为空指针nullptr
        if (0 == station_information_list->count()) {
            // 打开的文本内容为空
            delete station_information_list;
            station_information_list = nullptr;
        }

        return station_information_list;
    }

    // 只读模式读取文件失败
    return nullptr;
}

QString Data_processing::get_station_name(QString file_name_and_path)
{
    // 获取文件名称，格式为***.txt
    QString content = file_name_and_path.split("/").last();

    return content.replace(".txt", "", Qt::CaseInsensitive);
}

Information* Data_processing::split_data(QString content)
{
    QStringList list = content.split("-");

    Information* info(nullptr);
    if (2 == list.count()) {
        // 设置数据项
        info = new Information(list[0], list[1].toDouble());
        info->set_is_inter_modulation(false);
        info->set_inter_modulation_frequency(0.0);
    }

    return info;
}
